package com.lu.auth.handler;

import com.lu.common.api.system.RemoteLogService;
import com.lu.common.core.constant.Constants;
import com.lu.common.core.utils.StringUtils;
import com.lu.common.security.domain.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

/**
 * @program LuCloud-RuoYi
 * @description: 认证成功处理
 * @author: zhanglu
 * @create: 2020-07-02 11:27:00
 */
@Component
public class AuthenticationSuccessEventHandler implements ApplicationListener<AuthenticationSuccessEvent> {

    @Autowired(required = false)
    private RemoteLogService remoteLogService;

    @Override
    public void onApplicationEvent(AuthenticationSuccessEvent event) {
        Authentication authentication = (Authentication) event.getSource();
        if(StringUtils.isNotEmpty(authentication.getAuthorities())
            && authentication.getPrincipal() instanceof LoginUser){
            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
            String username = loginUser.getUsername();
            //记录用户登陆日志
            remoteLogService.saveLogininfor(username, Constants.LOGIN_SUCCESS, "登陆成功");
        }
    }

}
